package com.lmk.sc.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lmk.sc.entity.User;
import com.lmk.sc.service.UserService;

@RestController
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/api/user")
    public List<User> list(){
        // 方便后期测试负载均衡
        System.out.println("Provider8001被调用...");
        return userService.list();
    }

    @GetMapping("/api/user/{id}")
    public User get(@PathVariable("id") Integer id){
        return userService.get(id);
    }

    @PostMapping("/api/user")
    public Map<String, Object> post(@RequestBody User user){
        userService.save(user);

        Map<String, Object> result = new HashMap<>();
        result.put("status", "ok");
        return result;
    }

    @PutMapping("/api/user")
    public Map<String, Object> put(@RequestBody User user){
        userService.save(user);

        Map<String, Object> result = new HashMap<>();
        result.put("status", "ok");
        return result;
    }

    @DeleteMapping("/api/user/{id}")
    public Map<String, Object> delete(@PathVariable("id") Integer id){
        userService.delete(id);

        Map<String, Object> result = new HashMap<>();
        result.put("status", "ok");
        return result;
    }
}
